<template>
    <div>
        <el-row>
            <el-col :span="24">
                <div>
                    <el-button type="primary" @click="addBtn">新增</el-button>
                    <!-- <el-button type="danger" plain>批量删除</el-button> -->

                    <!-- 隐藏输入框 -->
                    <!-- <input type="file" ref="fileInput" style="display: none" @change="handleFileChange" /> -->

                    <!-- 点击触发文件选择的导入按钮 -->
                    <el-button plain>导入</el-button>

                    <el-button plain>导出</el-button>
                </div>
            </el-col>
        </el-row>
        <br>
        <el-row>
            <el-col :span="24">
                <el-form :inline="true" :model="param">

                    <el-form-item label="所属角色">
                        <el-select v-model="param.role" placeholder="请选择">
                            <el-option label="开发" value="开发"></el-option>
                            <el-option label="客服" value="客服"></el-option>
                            <el-option label="boss" value="boss"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="" label-width="100px">
                        <el-input placeholder="请输入关键词搜索" style="width: 200px; margin-left: 10px;"
                            v-model="param.departmentName">
                        </el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="queryAllPowerUser">查询</el-button>
                        <el-button style="margin-left: 10px;" @click="clearSearch">重置</el-button>
                    </el-form-item>

                </el-form>
            </el-col>
        </el-row>


        <el-row>
            <el-col :span="24">
                <div>
                    <el-table :data="tableData" style="width: 100%; margin-top: 20px;"
                        @selection-change="handleSelectionChange">
                        <el-table-column type="selection" width="55">
                        </el-table-column>
                        <el-table-column prop="id" label="员工ID" width="150"></el-table-column>

                        <el-table-column prop="username" label="员工账号" width="150"></el-table-column>

                        <el-table-column prop="realName" label="姓名" width="150"></el-table-column>

                        <el-table-column prop="locked" label="员工状态" width="150">
                            <template slot-scope="scope">
                                <span v-if="scope.row.locked == 0" style="color: green;">启用</span>
                                <span v-else-if="scope.row.locked == 1" style="color: red;">未启用</span>
                            </template>
                        </el-table-column>

                        <el-table-column prop="role" label="角色" width="140"></el-table-column>

                        <el-table-column prop="createdId" label="创建人" width="140"></el-table-column>

                        <el-table-column prop="createdTime" label="创建时间" width="150"></el-table-column>

                        
                        <el-table-column label="操作" width="180">                          
                            <template slot-scope="scope">
                                <el-button v-if="hasPermission('kaichu')" type="text" size="medium" style="color: red;"
                                    @click="deleteBtn(scope.row.id)">开除</el-button>

                                <el-switch v-model="scope.row.roleId" :active-value="1" :inactive-value="2"
                                    active-text="开发" inactive-text="客服"
                                    @change="(value) => handleSwitchChange(value, scope.row.id)"></el-switch>

                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="24">
                <!-- 分页部分 -->
                <div>
                    <el-pagination background :page-sizes="[5, 10, 20, 50]" :page-size="pageSize"
                        :current-page="pageNum" layout="total, sizes, prev, pager, next, jumper" :total="total"
                        @prev-click="prePage" @next-click="nextPage" @current-change="curPage"
                        @size-change="sizeChange">
                    </el-pagination>
                </div>
            </el-col>
        </el-row>



        <!-- 添加弹窗 -->
        <el-dialog title="新增" :visible.sync="addVisible">
            <el-form>
                <el-form-item label="角色" label-width="80px">
                    <el-select placeholder="请选择" style="width: 120px;" v-model="add.roleId">
                        <el-option label="开发" value="1"></el-option>
                        <el-option label="客服" value="2"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="员工账号" label-width="80px">
                    <el-input v-model="add.username"></el-input>
                </el-form-item>

                <el-form-item label="密码" label-width="80px">
                    <el-input v-model="add.password"></el-input>
                </el-form-item>

                <el-form-item label="姓名" label-width="80px">
                    <el-input v-model="add.realName"></el-input>
                </el-form-item>



            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
export default {
    data() {
        return {
            pageSize: 10,
            pageNum: 1,
            total: 0,

            tableData: [],
            param: {},
            multipleSelection: [],

            addVisible: false,
            add: {}
        }
    },

    methods: {
        handleSelectionChange(val) {
            //点击复选框的事件val：是选中的对象数组
            this.multipleSelection = val;
        },

        queryAllPowerUser() {
            let pageParam = {};
            pageParam.pageNum = this.pageNum;
            pageParam.pageSize = this.pageSize;
            pageParam.param = this.param;
            this.$axios.post('api/powerUser/queryAllPowerUser', pageParam)
                .then(resp => {
                    console.log(resp.data);
                    if (resp.data.code == 200) {
                        this.tableData = resp.data.data.list;
                        this.total = resp.data.data.total;
                    }
                })
        },

        clearSearch() {
            this.param = {};
            this.queryAllPowerUser();
        },

        deleteBtn(id) {
            this.$axios.get('api/powerUser/updateStatus/' + id)
                .then(response => {
                    if (response.data.code == 200) {
                        this.queryAllPowerUser();
                    }
                })
        },

        addBtn() {
            this.addVisible = true;
        },
        saveAdd() {
            this.$axios.post('api/powerUser/addPowerUser', this.add)
                .then(resposne => {
                    if (resposne.data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '成功!'
                        });
                    }
                })
        },

        handleSwitchChange(value, id) {
            // 构建 DTO 对象
            let powerUserDTO = {
                poweruserId: id,
                roleId: value // 使用开关的值来设置角色 ID
            };

            this.$axios.post('api/powerUser/updatePower', powerUserDTO)
                .then(response => {
                    if (response.data.code === 200) {
                        window.location.reload();
                        this.$message({
                            type: 'success',
                            message: '成功!'
                        });
                    }
                })
                .catch(error => {
                    console.error("更新失败:", error);
                    this.$message.error('更新失败!');
                });
        },
        //--------------------------------------------------------------

        // 点击上一页
        prePage() {
            if (this.pageNum > 1) {
                this.pageNum--;
                this.queryAllPowerUser(); // 更新数据
            }
        },

        // 点击下一页
        nextPage() {
            if (this.pageNum < Math.ceil(this.total / this.pageSize)) {
                this.pageNum++;
                this.queryAllPowerUser(); // 更新数据
            }
        },

        // 页码变化
        curPage(pageNum) {
            this.pageNum = pageNum;
            this.queryAllPowerUser(); // 更新数据
        },

        // 每页显示条数变化
        sizeChange(pageSize) {
            this.pageSize = pageSize;
            this.pageNum = 1; // 每页条数变化时，回到第一页
            this.queryAllPowerUser(); // 更新数据
        },


        // ------------------------------------------------------------------


        // 获取用户权限
        fetchUserPermissions(userIds) {
            this.$axios.get('api/menu/permissions/' + userIds)
                .then(response => {
                    if (response.data.code === 200) {
                        this.userPermissions = response.data.data; // 保存用户权限
                    }
                })
                .catch(error => {
                    console.error("获取用户权限失败:", error);
                });
        },
        // 检查用户是否有某个权限
        hasPermission(authCode) {
            return this.userPermissions.includes(authCode);
        },




    },
    created() {
        let userInfoString = localStorage.getItem('userInfo');
        let userInfo = JSON.parse(userInfoString);
        let userIds = userInfo.id;


        this.queryAllPowerUser();


        this.fetchUserPermissions(userIds);

    }
}
</script>

<style scoped></style>